#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

#create HDMI/DP config for xrandr to save hotplug status
HDMI_HOTPLUG_CONFIG="/boot/display/hdmi/hdmi_plug_flag.cfg"
DP_HOTPLUG_CONFIG="/boot/display/dp/dp_plug_flag.cfg"
HDMI_XRANDR_CONFIG="/boot/display/hdmi/xrandr.cfg"
DP_XRANDR_CONFIG="/boot/display/dp/xrandr.cfg"

if [ ! -d /boot/display ]; then
    mkdir /boot/display
	if [ ! -d /boot/display/hdmi ]; then
		mkdir /boot/display/hdmi
	fi

	if [ ! -d /boot/display/dp ]; then
		mkdir /boot/display/dp
	fi
fi

if [ -f $HDMI_HOTPLUG_CONFIG ]; then
	rm -rf $HDMI_HOTPLUG_CONFIG
	rm -rf $HDMI_XRANDR_CONFIG
fi

if [ -f $DP_HOTPLUG_CONFIG ]; then
	rm -rf $DP_HOTPLUG_CONFIG
	rm -rf $DP_XRANDR_CONFIG
fi

touch $HDMI_HOTPLUG_CONFIG
touch $DP_HOTPLUG_CONFIG
touch $HDMI_XRANDR_CONFIG
touch $DP_XRANDR_CONFIG

# Generate the SSH keys if non-existent
if [ ! -f /etc/ssh/ssh_host_rsa_key ]
then
    # else ssh service start in dpkg-reconfigure will fail
    systemctl stop ssh.socket||true
    dpkg-reconfigure openssh-server
fi

# Start switch sound output devices at boot time
systemctl restart switch-sound-device-boot.service
systemctl stop switch-sound-device-boot.service

exit 0
